package com.hd.crdao.wrapper;

import com.google.gson.Gson;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Android
 * @date 2018/1/23
 */

public abstract class UpFileCallBack extends StringCallback {


    @Override
    public void inProgress(float progress, long total, int id) {
        ALog.d("" + progress);
        if (progress == 1) {        //表示文件已经上传完成
            onUpFileFinished();
            ALog.d(progress + "完成上传的回调");
        }
        super.inProgress(progress, total, id);
    }

    /**
     * 文件已经上传完成的回调
     */
    public abstract void onUpFileFinished();


    public UpFileCallBack() {
        super();
    }

    /**
     * 操作已经完成的操作
     *
     * @param id
     */
    @Override
    public void onAfter(int id) {
        super.onAfter(id);
        ALog.d("onAfter调用");
    }

    @Override
    public String parseNetworkResponse(Response response, int id) throws IOException {
        return super.parseNetworkResponse(response, id);
    }


    @Override
    public void onError(Call call, Exception e, int id) {
        call.cancel();
        MyToast.show("网络开了个小差");
        ALog.d(e.getMessage() + "-----");
    }

    @Override
    public void onResponse(String response, int id) {
        MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
        if (msgBean.getSuccess() == 0) {
            onSuccess(msgBean.getMessage());
        } else {
            onFailed(msgBean.getMessage());
        }
    }

    /**
     * 返回是失败的回调
     *
     * @param message
     */
    protected abstract void onFailed(String message);

    /**
     * 返回的是成功的回调
     *
     * @param message
     */
    protected abstract void onSuccess(String message);
}
